<?php

include '../../db.php';
$connect = pg_connect($connectionString);
if (!$connect) {
    echo "connect : A connection to the server could not be established!";
    return false;
}
$header = $_POST['header'];
$params = array();
array_push($params, pg_escape_string($header['emission_date']));
array_push($params, pg_escape_string($header['client_id']));
array_push($params, pg_escape_string($header['total']));
array_push($params, pg_escape_string($header['serie']));
array_push($params, pg_escape_string($header['number']));
array_push($params, pg_escape_string($header['cancellation_date']));
array_push($params, pg_escape_string($header['igv']));
array_push($params, pg_escape_string($header['subtotal']));

pg_query("BEGIN");
$stmt = pg_prepare($connect, "create_scaffold", "INSERT INTO sale_header(
            emission_date, client_id, total, serie, number, cancellation_date, 
            igv, subtotal)  VALUES ($1, $2, $3, $4, $5, $6, $7, $8);");
$result = pg_execute($connect, "create_scaffold", $params);

if (!$result) {
    die("Error in SQL query: " . pg_last_error());
}
$last_id_query = pg_query("SELECT currval('sale_header_id_seq')");

$last_id_results = pg_fetch_assoc($last_id_query);

$id_header = $last_id_results['currval'];
//------------------------------------------------------------------------------
$detail = $_POST['details'];

$stmt = pg_prepare($connect, "create_scaffold2", "INSERT INTO sale_detail(
             product_id, quantity, price, total,header_id)  VALUES ($1, $2, $3, $4, $5);");
$t = count($detail);
for ($i = 0; $i < $t; $i++) {
    $params = array();
    array_push($params, pg_escape_string($detail[$i]['id']));
    array_push($params, pg_escape_string($detail[$i]['quantity']));
    array_push($params, pg_escape_string($detail[$i]['price']));
    array_push($params, pg_escape_string($detail[$i]['total']));
    array_push($params, pg_escape_string($id_header));
    $result = pg_execute($connect, "create_scaffold2", $params);
    //--------------------------------------------------------------------------
    $params4 = array();
    array_push($params4, pg_escape_string($detail[$i]['id']));

    $stmt4 = pg_prepare($connect, "select_product", "SELECT quantity FROM warehouse_product WHERE id = $1 ");
    $result1 = pg_execute($connect, "select_product", $params4);
    $quantity = pg_fetch_all($result1);
    //print_r($quantity[0]['quantity']) ;
    // //--------------------------------------------------------------------------
    $params3 = array();
    array_push($params3, pg_escape_string($detail[$i]['id']));
    array_push($params3, pg_escape_string(floatval($quantity[0]['quantity'])-floatval($detail[$i]['quantity'])));


    $stmt3 = pg_prepare($connect, "update_product", "UPDATE warehouse_product SET quantity=$2  WHERE id = $1 ");
    $result = pg_execute($connect, "update_product", $params3);
//------------------------------------------------------------------------------
    $params2 = array();
    array_push($params2, pg_escape_string($header["emission_date"]));
    array_push($params2, pg_escape_string(2));
    array_push($params2, pg_escape_string($detail[$i]['price']));
    array_push($params2, pg_escape_string($detail[$i]['quantity']));
    array_push($params2, pg_escape_string($detail[$i]['total']));
    array_push($params2, pg_escape_string($detail[$i]['id']));
    array_push($params2, pg_escape_string($id_header));
    
    array_push($params2, pg_escape_string(floatval($quantity[0]['quantity'])-floatval($detail[$i]['quantity'])));

    $stmt2 = pg_prepare($connect, "create_move", "INSERT INTO warehouse_move(move_date,type,price,quantity,total,product_id,header_id,stock) "
            . "VALUES ($1,$2,$3,$4,$5,$6,$7,$8);");
    $result = pg_execute($connect, "create_move", $params2);
       
}



pg_query("COMMIT");
pg_close($connect);




//echo json_encode($_POST['details']);